<template>
    <el-dropdown trigger="click">
        <el-badge :value="total" :max="99" class="item" :hidden="total == 0">
            <div class="edu-msg"></div>
        </el-badge>
        <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="item in messages" :key="item.linkId">
                <div
                    @click="dynamicJump(item)"
                >
                    <div class="edu-msg-item">
                        <div class="itme-icon"></div>
                        <div class="item-text">
                            <span class="item-title">{{item.msgTitle}}</span>
                            <span class="item-date">{{item.createTime}}</span>
                        </div>
                    </div>
                </div>
            </el-dropdown-item>
            <el-dropdown-item>
                <div class="edu-msg-clean" v-if="messages.length > 0">
                    <router-link to="/messages">查看更多</router-link>
                </div>
                <div class="edu-msg-empty" v-else>
                    <span>暂无消息</span>
                </div>
            </el-dropdown-item>
        </el-dropdown-menu>
    </el-dropdown>
</template>
<script>
import {
    countCommonMessage,
    findTopFiveCommonMessage
} from "common/src/api/public/message";
import bus from 'common/src/utils/bus';
export default {
    name: "MsgList",
    data() {
        return {
            messages: [],
            total: 0
        };
    },
    created() {
        bus.$on('update_count_msg', () => {
            this.init();
        })
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            findTopFiveCommonMessage().then(res => {
                if (res.code == 200) {
                    this.messages = res.data.map(ele => {
                        ele.createTime = new Date(
                            ele.createTime
                        ).toLocaleDateString();
                        return ele;
                    });
                }
            });
            countCommonMessage().then(res => {
                if (res.code == 200) this.total = res.data;
            });
        },
        dynamicJump(message) {
            let path
            switch (message.msgType) {
                case '1':
                    path = '/messageDetail/'
                    break;
                case '2':
                    path = `/TeacherRollDetail?id=${message.linkId}`
                    break;
                case '3':
                    path = ''
                    this.$router.push({
                        name: message.linkId,
                        params: {
                            id: message.id,
                            msgSource: message.msgSource
                        }
                    })
                    break;
                default:
                    this.$message({
                        type: 'warning',
                        message: '未适配的消息类型'
                    })
                    path = ''
                    break;
            }
            if (path) {
                this.$router.push({
                    path: path
                });
            }
        }
    }
};
</script>
<style lang="scss" scoped>
.el-dropdown-menu {
    > .under-line {
        border-bottom: 1px solid #9999;
    }
}
.edu-msg {
    cursor: pointer;
    width: 16px;
    height: 16px;
    margin-top: 6px;
    background: url("/static/images/btn_todo.png");
    &:hover {
        background: url("/static/images/btn_todo_hover.png");
    }
}
.item {
    height: 18px;
    margin-right: 10px;
}
.edu-msg-item {
    width: 240px;
    padding: 8px 0 8px;
    border-bottom: 1px solid #ddd;
    .itme-icon {
        float: left;
        width: 24px;
        height: 24px;
        margin-right: 12px;
        margin-top: 6px;
        background: url("/static/images/icon_systemnotice.png");
        background-size: cover;
    }
    .item-text {
        height: 40px;
        overflow: hidden;
        > span {
            display: block;
        }
        .item-title {
            display: block;
            height: 20px;
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            color: #666666;
        }
        .item-date {
            color: #999999;
            font-size: 12px;
        }
    }
}
.edu-msg-clean {
    text-align: center;
    padding: 16px 0 12px;
}
.edu-msg-empty {
    text-align: center;
    width: 240px;
    height: 80px;
    line-height: 80px;
    color: #999999;
}
</style>
